import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: null,
    userType: null,
    user: null
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
    },
    SET_USER_TYPE(state, userType) {
      state.userType = userType
    },
    SET_USER(state, user) {
      state.user = user
    },
    CLEAR_AUTH(state) {
      state.token = null
      state.userType = null
      state.user = null
    }
  },
  getters: {
    token: state => state.token,
    userType: state => state.userType,
    user: state => state.user
  },
  actions: {
    login({ commit }, { token, userType, user }) {
      commit('SET_TOKEN', token)
      commit('SET_USER_TYPE', userType)
      commit('SET_USER', user)
    },
    logout({ commit }) {
      commit('CLEAR_AUTH')
    }
  }
})
